package com.hoodbrains.insideloc.shopping;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.hoodbrains.insideloc.R;
import com.hoodbrains.insideloc.utils.Catalog;

public class ProductChoiceActivity extends ListActivity {
	Context mContext;

	private class ProductAdapter extends ArrayAdapter<String> {
		private LayoutInflater inflater;
		private int color;

		public ProductAdapter(ProductChoiceActivity productChoiceActivity,
				int productChoice, ArrayList<String> products, int color) {
			super(productChoiceActivity, productChoice, products);
			inflater = LayoutInflater.from(productChoiceActivity);
			this.color = color;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.rayon_list_item,
						parent, false);
			}

			TextView name = (TextView) convertView.findViewById(R.id.rayon_sli);

			name.setText(this.getItem(position));
			

			name.setTextColor(color);
			//name.setTextSize(19);

			return convertView;

		}

	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		mContext = this;
		
		getListView().setBackgroundResource(R.drawable.background);
		getListView().setCacheColorHint(0x00000000);

		setListAdapter(new ProductAdapter(this, R.layout.product_choice, 
				Catalog.getInstance().getProducts(this.getIntent().getStringExtra("type")),
				Color.parseColor(Catalog.getInstance().getColor(this.getIntent().getStringExtra("type")))));
		
		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
		      // When clicked, show a toast with the TextView text
		    	Intent resultValue = new Intent();
		    	resultValue.putExtra("product", (String)lv.getAdapter().getItem(position));
		    	resultValue.putExtra("rayon", ((Activity) mContext).getIntent().getStringExtra("type"));
		    	setResult(RESULT_OK, resultValue);
		    	finish();
		    }
		  });
	}
}
